require 'rubygems'
gem "rspec", "<=1.2.9"
require 'selenium/rake/tasks'
require 'spec/rake/spectask'
require 'fileutils'
require 'yaml'
require "net/http"
require "ruby2jar"
CONFIG = YAML.load_file 'config.yml'

#require 'rubygems'
#require 'rake'
#require 'rake/clean'
#require 'rake/gempackagetask'
#require 'rake/rdoctask'
#require 'rake/testtask'
#require 'spec/rake/spectask'

Selenium::Rake::RemoteControlStartTask.new(:'rc:start') do |rc|
  rc.port = 4444
  rc.timeout_in_seconds =  4 * 60
  rc.background = true
  rc.wait_until_up_and_running = true
  rc.jar_file = CONFIG['selenium_server_jar']
  rc.additional_args << "-firefoxProfileTemplate #{CONFIG['firefox_profile']}" if CONFIG['firefox_profile']
end

Selenium::Rake::RemoteControlStopTask.new(:'rc:stop') do |rc|
  rc.host = "localhost"
  rc.port = 4444
  rc.timeout_in_seconds = 4 * 60
end
desc "Restart Selenium Remote Control"
task :'rc:restart' do
  Rake::Task[:'rc:stop'].execute [] rescue nil
  Rake::Task[:'rc:start'].execute []
end

desc "Deletes the 'target' directory"
task :'clean' do
  FileUtils.rm_rf('target') if File.directory?('target')
end

desc "Run all spec files"
time = Time.new.strftime("%Y%m%d%a%H%M")
Spec::Rake::SpecTask.new('run_all_spec') do |t|
      t.libs << "spec"
      t.pattern = "spec/*_spec.rb"
      t.spec_opts << "--color"
      t.spec_opts << "--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter'"
      t.spec_opts << "--format=Selenium::RSpec::SeleniumTestReportFormatter:./target/SLMC_ALL_SPEC#{time.upcase}.html"
      t.spec_opts << "--format=specdoc"
      t.verbose = true
end

desc "my run"
time = Time.new.strftime("%Y%m%d%a%H%M")
Spec::Rake::SpecTask.new('my_run') do |t|
      t.libs << "spec"
      t.pattern = "spec/*_spec.rb"
     # t.pattern = a1.flatten!
      t.spec_opts << "--color"
      t.spec_opts << "--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter'"
      t.spec_opts << "--format=Selenium::RSpec::SeleniumTestReportFormatter:./target/SLMC_ALL_SPEC#{time.upcase}.html"
      t.spec_opts << "--format=specdoc"
      t.verbose = true
end

def new_selenium_spec_task(spec_name, spec_file)
  time = Time.new.strftime("%Y%m%d%a%H%M")
  desc "#{spec_name.upcase} Spec Task"
  Spec::Rake::SpecTask.new(:"#{spec_name}") do |t|
    t.libs << "spec"
    t.pattern = "spec/#{spec_file}"
    t.spec_opts << "--color"
    t.spec_opts << "--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter'"
    t.spec_opts << "--format=Selenium::RSpec::SeleniumTestReportFormatter:./target/SLMC_#{spec_name.upcase}_#{time.upcase}.html"
    t.spec_opts << "--format=specdoc"
    t.verbose = true
  end
end
wd = Dir.pwd
Dir.chdir(wd + '/spec')
def create_spec_tasks
  spec_files =  Dir.glob("*_spec.rb")
  spec_names = []
  spec_files.length.times do |x|
    spec_names << spec_files[x].split('_spec.rb')
  end
  spec_names.flatten!
  spec_names.length.times do |x|
    new_selenium_spec_task("#{spec_names[x]}","#{spec_files[x]}")
  end
end
create_spec_tasks
Dir.chdir(wd)
#new_selenium_spec_
#  spec_files =  Dir.glob("*_spec.rb")
#  puts spec_files
##    new_selenium_spec_task("ss","test_one_spec.rb")
##create_spec_tasks
#wd = Dir.pwd
#puts wd v
#ss = Dir.chdir(wd + '/spec')
#s =  Dir.glob("*_spec.rb")
#puts


#create_spec_tasks
#new_selenium_spec_task("ss","test_one_spec.rb")
### existing ######################################

##spec = Gem::Specification.new do |s|
##  s.name = 'selenium_template'
##  s.version = '0.0.1'
##  s.has_rdoc = true
##  s.extra_rdoc_files = ['README', 'LICENSE']
##  s.summary = 'Your summary here'
##  s.description = s.summary
##  s.author = ''
##  s.email = ''
##  # s.executables = ['your_executable_here']
##  s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
##  s.require_path = "lib"
##  s.bindir = "bin"
##end
##
##Rake::GemPackageTask.new(spec) do |p|
##  p.gem_spec = spec
##  p.need_tar = true
##  p.need_zip = true
##end
##
##Rake::RDocTask.new do |rdoc|
##  files =['README', 'LICENSE', 'lib/**/*.rb']
##  rdoc.rdoc_files.add(files)
##  rdoc.main = "README" # page to start on
##  rdoc.title = "selenium_template Docs"
##  rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
##  rdoc.options << '--line-numbers'
##end
##
##Rake::TestTask.new do |t|
##  t.test_files = FileList['test/**/*.rb']
##end
##
##Spec::Rake::SpecTask.new do |t|
##  t.spec_files = FileList['spec/**/*.rb']
##  t.libs << Dir["lib"]
##end
### existing ######################################